//
//  IndustryViewController.m
//  FKGW
//
//  Created by song ce on 2017/2/16.
//  Copyright © 2017年 song ce. All rights reserved.
//

#import "IndustryViewController.h"
#import "MenuView.h"
#import "TopCell.h"
#import "IndustryCell.h"
#import "NoDataCell.h"
#import "StoreViewController.h"

@interface IndustryViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,assign)BOOL isDApageContainerRight;//记录是否加载过数据
@property(nonatomic,strong)NSMutableArray *industrys;
@property(nonatomic,strong)MenuView *menuV;
@property(nonatomic,assign)BOOL isNoData;
@end

@implementation IndustryViewController


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    DLog(@"行业将要出现");
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //数据源
    self.industrys = [NSMutableArray array];
    self.dataSource = [NSMutableArray array];
    //表示图
    [self tableViewAndHeaderView];

//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(loadData) name:@"cityInfo" object:nil];
    [[NSNotificationCenter defaultCenter ]addObserver:self selector:@selector(ll) name:@"DAPagesContainerHome" object:nil];
    [self loadActivityStores];
}

-(void)loadActivityStores{
//    NSString *cityId =  [AppDelegate appDealegate].cityId;
    //活动店铺
    NSDictionary *paraDic = @{
                              @"cityId":@"",
                              @"size":@"8",
                              @"currentpage":@"1"
                              };
    __weak typeof(self)  weakSelf = self;
    [Tools AFNetworkingRequestTypePostWithUrl:EVENTSTORE paramters:paraDic  afterResultBlock:^(NSDictionary *resultDic) {
        DLog(@"活动店铺接口----%@",resultDic);
        if([resultDic[@"flag"] isEqualToString:@"true"]){
            NSDictionary *dataDic = resultDic[@"data"];
            if([dataDic isKindOfClass:[NSNull class]]) return ;
                    NSArray *eventStore = dataDic[@"stores"];
                    for(NSDictionary *eventDic in eventStore){
                        IndustryModel *model = [[IndustryModel alloc]init];
                        model.storeId = eventDic[@"storeId"];
                        model.storeName = eventDic[@"storeName"];
                        NSArray *pics = eventDic[@"pics"];
//                        model.pics = @[@"merchant/market/4.jpg",@"merchant/market/8.jpg"];
                        model.pics = pics;
                        [weakSelf.dataSource addObject:model];
                    }
            if(!weakSelf.dataSource.count){
                weakSelf.isNoData = YES;
                [weakSelf.dataSource addObject:@""];
            }
            if(weakSelf.dataSource.count<2){
                weakSelf.tableV.height = Main_Screen_Height-kNaviBarHeight-Main_Screen_Width/2.28-44-49-1+9;
            }
                    [weakSelf.tableV reloadData];
        }else{
            if(resultDic[@"errorString"]){
                [MBProgressHUD showTipMessageInWindow:resultDic[@"errorString"]];
            }
        }
    }];
}
-(void)ll{
    if(self.isDApageContainerRight) return;
    self.isDApageContainerRight = YES;
    [self loadData];
    DLog(@"行业将要出现");
}
-(void)loadData{
//      NSString *cityId =  [AppDelegate appDealegate].cityId;
    //2.16首页行业展示接口
      NSDictionary *paraDic = @{
                              @"cityId":@"",
                              @"size":@"7",
                              @"currentpage":@"1"
                              };
    __weak typeof(self)  weakSelf = self;
    [Tools AFNetworkingRequestTypePostWithUrl:HOMEPAGEBYTRADE paramters:paraDic  afterResultBlock:^(NSDictionary *resultDic) {
        DLog(@"首页行业展示接口----%@",resultDic);
        if([resultDic[@"flag"] isEqualToString:@"true"]){
            NSDictionary *dataDic = resultDic[@"data"];
            if([dataDic isKindOfClass:[NSNull class]]) return ;
            NSArray *industrys = dataDic[@"industrys"];
            for(NSDictionary *indic in industrys){
                MenuItem *item = [[MenuItem alloc]init];
                item.logo = indic[@"logo"];
                item.name = indic[@"name"];
                item.tradeId =  indic[@"tradeId"];
                [weakSelf.industrys addObject:item];
            }
            weakSelf.menuV.dataSoure = weakSelf.industrys;
        }
  }];
}

- (void)tableViewAndHeaderView{
    //表视图
    self.tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, 1, Main_Screen_Width, Main_Screen_Height-49-60-44) style:UITableViewStylePlain];
    self.tableV.showsVerticalScrollIndicator = NO;
    self.tableV.backgroundColor = DEF_COLOR242;
    self.tableV.delegate = self;
    self.tableV.dataSource =self;
    self.tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableV];
    
   CGFloat menuViewHeight = 176;
    MenuView *menuView = [[MenuView alloc]initWithFrame:CGRectMake(0, 3,Main_Screen_Width, menuViewHeight)];
    menuView.backgroundColor = DEF_COLOR242;
    self.tableV.tableHeaderView = menuView;
    self.menuV = menuView;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count+1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row==0){
        NSString *ident=@"acell";
        TopCell *cell=[tableView dequeueReusableCellWithIdentifier:ident];
        if(cell==nil){
            cell=[[[NSBundle mainBundle]loadNibNamed:@"TopCell" owner:self options:nil]lastObject];
        }
        return cell;
    }
    if(indexPath.row==1&&self.isNoData){
        NSString *ident=@"nodatacell";
        NoDataCell *cell=[tableView dequeueReusableCellWithIdentifier:ident];
        if(cell==nil){
            cell=[[[NSBundle mainBundle]loadNibNamed:@"NoDataCell" owner:self options:nil]lastObject];
        }
        return cell;
    }
    static NSString *iden = @"shop_cell";
    IndustryCell *cell1 = [tableView dequeueReusableCellWithIdentifier:iden];
    if(cell1==nil){
        cell1=[[[NSBundle mainBundle]loadNibNamed:@"IndustryCell" owner:self options:nil]lastObject];
    }
    IndustryModel *model = self.dataSource[indexPath.row-1];
    cell1.model = model;
    return cell1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row==0){
        return 46;
    }
    return 163;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//        DLog(@"-self.tableV.height-%f", self.tableV.height);
//        DLog(@"-self.tableV.contentSize.height-%f", self.tableV.contentSize.height);
//        DLog(@"-scrollView.contentOffset.y-***%f",scrollView.contentOffset.y);
    if(scrollView.contentOffset.y+self.tableV.height>=self.tableV.contentSize.height){
        if(self.dataSource.count<3){
            return;
        }

        [[NSNotificationCenter defaultCenter]postNotificationName:@"scrollToBottom" object:nil];
    }
    if(scrollView.contentOffset.y<0){
            [[NSNotificationCenter defaultCenter]postNotificationName:@"scrollToTop" object:nil];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.row==0) return;
    if(indexPath.row==1&&self.isNoData) return;
    IndustryModel *shopModel = self.dataSource[indexPath.row-1];
    StoreViewController *goodsDetail  = [[StoreViewController alloc]init];
    goodsDetail.storeId =shopModel.storeId ;
    goodsDetail.vcName = shopModel.storeName;
    goodsDetail.hidesBottomBarWhenPushed = YES;
    [[self secondNavigationController] pushViewController:goodsDetail animated:YES];
 }

- (BaseNavigationViewController*)secondNavigationController{
    UIResponder *next = self.nextResponder;
    while (next!=nil) {
        if([next isKindOfClass:[BaseNavigationViewController class]]){
            return (BaseNavigationViewController*)next;
        }
        next = next.nextResponder;
    }
    return nil;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
